package com.igeekhome.ccs.controller;
import com.igeekhome.ccs.biz.ICsCustomerBiz;
import com.igeekhome.ccs.biz.ICustomerBiz;

import com.igeekhome.ccs.biz.ICustomerServiceBiz;
import com.igeekhome.ccs.biz.IJobBiz;

import com.igeekhome.ccs.entity.CsCustomer;
import com.igeekhome.ccs.entity.Customer;
import com.igeekhome.ccs.entity.CustomerService;
import com.igeekhome.ccs.entity.Job;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;


//客户页面
@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    ICustomerBiz iCustomerbiz;
    @Autowired
    IJobBiz iJobbiz;
    @Autowired
    ICustomerServiceBiz iCustomerServiceBiz;
    @Autowired
    ICsCustomerBiz iCsCustomerBiz;
    /**
     * 这里是查询客户信息功能
     */

    @RequestMapping("/select")
    public @ResponseBody
    List<Customer> select( @RequestBody Map<String, String> map) {

        Customer cs=new Customer();
        if(map.get("searcher")!=null){
            cs.setCustomerId(Integer.parseInt(map.get("searcher")));
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = null;
        Date endTime = null;
        List<Customer> list = null;
        CustomerService cs1 = iCustomerServiceBiz.getOneByUsername((String) SecurityUtils.getSubject().getPrincipal());
        cs.setCsId(cs1.getCsId());
        if(map.get("start")==null&&map.get("end")==null){
            cs.setGrade(map.get("grade"));
            cs.setSourceWay(map.get("sourceWay"));
            list=iCustomerbiz.select(cs);
        }else{
            try {
                startTime = simpleDateFormat.parse(map.get("start"));
                endTime = simpleDateFormat.parse(map.get("end"));
                cs.setEndTime(endTime);
                cs.setStartTime(startTime);
                cs.setGrade(map.get("grade"));
                cs.setSourceWay(map.get("sourceWay"));
                System.out.println(map.get("sourceWay"));
                list=iCustomerbiz.select(cs);
                return list;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    /**
     *新建客户功能
     */
    @RequestMapping("newcustomer")
    public @ResponseBody
    String save(@RequestBody Map<String, String> map){
        Customer cs=new Customer();
        Random random = new Random();
        Customer cshelper=new Customer();
        CustomerService cs1 = iCustomerServiceBiz.getOneByUsername((String) SecurityUtils.getSubject().getPrincipal());
        cshelper.setCsId(cs1.getCsId());
        List<Customer> bean=iCustomerbiz.select(cshelper);
        boolean choose=false;
        int i=0;
        while(choose==false){
                i = random.nextInt(1000);
            for(int j=0;j<bean.size();j++){
                if(bean.get(j).getCustomerId()==i){
                    choose=false;break;
                }
                if(j==bean.size()-1&&bean.get(j).getCustomerId()!=i){
                    choose=true;break;
                }
            }
        }
        CsCustomer csc=new CsCustomer();
        csc.setCsId(cs1.getCsId());
        csc.setCustomerId(i);
        Date date = new Date();
        cs.setCustomerId(i);
        cs.setCreateTime(date);
        cs.setCustomerName(map.get("customerName"));
        cs.setRealName(map.get("realName"));
        cs.setPhone(map.get("phone"));
        cs.setEmail(map.get("email"));
        cs.setCompany(map.get("company"));
        cs.setAddress(map.get("address"));
        cs.setLabelId(Integer.parseInt(map.get("labelId")));
        cs.setGrade(map.get("grade"));
        cs.setSourceWay(map.get("sourceWay"));
        cs.setRemark(map.get("remark"));
        cs.setCsId(cs1.getCsId());
        iCustomerbiz.save(cs);
        iCsCustomerBiz.save(csc);
        return null;
    }
    /**
     *创建工单
     */
    @RequestMapping("newjob")
    public @ResponseBody
    String newjob(@RequestBody Map<String, String> map){
        Job jb=new Job();
        Random random = new Random();
        boolean choose=false;
        int i=0;
        Job jbhelper=new Job();
        List<Job> jbreturn;
        while(choose==false){
            i = random.nextInt(1000);
            jbhelper.setJobId(i);
            jbreturn=iJobbiz.select(jbhelper);
            if(jbreturn.size()==0){
                choose=true;
                break;
            }
        }
        jb.setJobId(i);
        jb.setJobSortId(Integer.parseInt(map.get("jobSortId")));
        jb.setStatus(map.get("status"));
        jb.setTitle(map.get("title"));
        jb.setContent(map.get("content"));
        jb.setSponsorCsId(Integer.parseInt(map.get("sponsorCsId")));
        jb.setPriority(Integer.parseInt(map.get("priority")));
        CustomerService cs = iCustomerServiceBiz.getOneByUsername((String) SecurityUtils.getSubject().getPrincipal());
        jb.setCsId(cs.getCsId());
        Date date=new Date();
        System.out.println(date);
        jb.setCreateTime(date);
        jb.setCustomerId(Integer.parseInt(map.get("customerId")));
        iJobbiz.newjob(jb);
        return null;
    }
    /**
     * 删除客户
     */
    @RequestMapping("deletecustomer")
    public @ResponseBody
    int deletecustomer(@RequestBody Map<String, String> map){
        iCustomerbiz.deletecustomer(Integer.parseInt(map.get("customerId")));
        return 0;
    }
    /**
     * 批量删除客户
     */
    @RequestMapping("deletecustomers")
    public @ResponseBody
    int deletecustomers(@RequestBody List<String> list){
        for(int i=0;i<list.size();i++){
            iCustomerbiz.deletecustomer(Integer.parseInt(list.get(i)));
        }
        return 0;
    }
}
